<%@ page import="javax.security.auth.login.LoginContext"%>
<%@ page import="org.ow2.bonita.facade.QueryRuntimeAPI"%>
<%@ page import="org.ow2.bonita.light.LightProcessInstance"%>
<%@ page import="org.ow2.bonita.facade.uuid.ProcessDefinitionUUID"%>
<%@page import="org.ow2.bonita.facade.uuid.ProcessInstanceUUID"%>
<%@ page import="org.ow2.bonita.util.AccessorUtil"%>
<%@ page import="org.ow2.bonita.util.SimpleCallbackHandler"%>
<%@page import="java.util.List"%>
<%@page import="java.util.Collections"%>

<%
final Object isAdmin = request.getSession().getAttribute("isadmin");
if (isAdmin == null || !((Boolean) isAdmin).booleanValue()) {
  response.sendRedirect("home.jsp");
}

final String msg = request.getParameter("msg");

final int numberOfElementsToRetrive = 20;
List<LightProcessInstance> journalInstances = Collections.emptyList();
List<LightProcessInstance> historyInstances = Collections.emptyList();
try {
  final String username = (String) request.getSession().getAttribute("username");
  final LoginContext loginContext = new LoginContext("BonitaStore", new SimpleCallbackHandler(username, ""));
  loginContext.login();
  final QueryRuntimeAPI journalQueryRuntimeAPI = AccessorUtil.getQueryRuntimeAPI(AccessorUtil.QUERYLIST_JOURNAL_KEY);
  final QueryRuntimeAPI historyQueryRuntimeAPI = AccessorUtil.getQueryRuntimeAPI(AccessorUtil.QUERYLIST_HISTORY_KEY);
  
  journalInstances = journalQueryRuntimeAPI.getLightProcessInstances(0, numberOfElementsToRetrive);
  historyInstances = historyQueryRuntimeAPI.getLightProcessInstances(0, numberOfElementsToRetrive);

  loginContext.logout();
} catch (Exception e) {
  e.printStackTrace();
  Throwable t = e;
  while (t.getCause() != null) {
    t = t.getCause();
  }
  final String errorMsg = "Error while listing instances: " + t.getMessage();
  response.sendRedirect("home.jsp?msg=" + errorMsg);  
}
%>
<jsp:include page="header.html"/>
<%if (msg != null) {%>
  <b><%=msg %></b><br><br>
<%}%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Instances</title>
</head>
<body>
<h1> INSTANCE PROCESSES</h1>

<a href="home.jsp">Home</a>&nbsp;&nbsp;&nbsp;
<a href="processes.jsp">Manage processes</a>&nbsp;&nbsp;&nbsp;
<a href="HomeBAM.jsp">Business Activity Monitoring</a>&nbsp;&nbsp;&nbsp;
<br><br>
<TABLE BORDER="1" CELLPADDING="10" WIDTH="600px">
<CAPTION>List of journal instances (max: <%=numberOfElementsToRetrive %>)</CAPTION>
<TR>
  <TH>Process Name</TH>
  <TH>Version</TH>
  <TH>Instance number</TH>
  <TH>Actions</TH>
</TR>
<%
for (LightProcessInstance instance : journalInstances) {
  final ProcessInstanceUUID instanceUUID = instance.getUUID();
  final ProcessDefinitionUUID processUUID = instance.getProcessDefinitionUUID();
  final String processName = processUUID.getProcessName();
  final String processVersion = processUUID.getProcessVersion();
  final long instanceNb = instanceUUID.getInstanceNb();
%>
<TR>
<TD><%=processName %></TD>
<TD align="center"><%=processVersion %></TD>
<TD align="center"><%=instanceNb %></TD>
<TD>
<a href="actions/apiCall.jsp?action=cancelInstance&back=instances&instanceUUID=<%=instanceUUID %>">Cancel</a>&nbsp;
<a href="actions/apiCall.jsp?action=deleteInstance&back=instances&instanceUUID=<%=instanceUUID %>">Delete</a>&nbsp;
</TD>
</TR>
<%}%>
</TABLE>

<br><br>

<TABLE BORDER="1" CELLPADDING="10" WIDTH="600px">
<CAPTION>List of history instances (max: <%=numberOfElementsToRetrive %>)</CAPTION>
<TR>
  <TH>Process Name</TH>
  <TH>Version</TH>
  <TH>Instance number</TH>
  <TH>Actions</TH>
</TR>
<%
for (LightProcessInstance instance : historyInstances) {
  final ProcessInstanceUUID instanceUUID = instance.getUUID();
  final ProcessDefinitionUUID processUUID = instance.getProcessDefinitionUUID();
  final String processName = processUUID.getProcessName();
  final String processVersion = processUUID.getProcessVersion();
  final long instanceNb = instanceUUID.getInstanceNb();
%>
<TR>
<TD><%=processName %></TD>
<TD align="center"><%=processVersion %></TD>
<TD align="center"><%=instanceNb %></TD>
<TD>
<a href="actions/apiCall.jsp?action=deleteInstance&back=instances&instanceUUID=<%=instanceUUID %>">Delete</a>&nbsp;
</TD>
</TR>
<%}%>
</TABLE>

</body>
</html>
<jsp:include page="footer.html"/>